package com.example.asus.picselector.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.example.asus.picselector.R;
import com.example.asus.picselector.widget.Util;
import com.example.asus.picselector.bean.UploadPicInfo;
import com.example.asus.picselector.listener.OnItemClickListener;

import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by Asus on 2018/4/9.
 */

public class UploadPicAdapter extends RecyclerView.Adapter<UploadPicAdapter.ViewHolder> {

    private Context context;
    private LayoutInflater layoutInflater;
    private boolean showDelete;
    private List<UploadPicInfo> uploadPicInfoList;
    private OnItemClickListener onItemClickListener;

    public UploadPicAdapter(Context context) {
        this(context, true);
    }

    public UploadPicAdapter(Context context, boolean showDel) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        showDelete = showDel;
    }

    public void setData(List<UploadPicInfo> uploadPicInfoList) {
        this.uploadPicInfoList = uploadPicInfoList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.rlv_item_upload_pic, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UploadPicInfo uploadPicInfo = uploadPicInfoList.get(position);
        if (uploadPicInfo != null)
            holder.setData(uploadPicInfo);
        holder.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return uploadPicInfoList != null ? uploadPicInfoList.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView picUploadImg;
        private ImageView delPicUploadImg;
        private ImageView addPicUploadImg;
        private OnItemClickListener mOnItemClickListener;

        public ViewHolder(View itemView) {
            super(itemView);
            picUploadImg = itemView.findViewById(R.id.img_pic_upload);
            delPicUploadImg = itemView.findViewById(R.id.img_delete_upload);
            addPicUploadImg = itemView.findViewById(R.id.img_delete_upload_add);
        }

        public void setData(UploadPicInfo uploadPicInfo) {
            String picUrl = uploadPicInfo.getPicUrl();
            if (getAdapterPosition() == uploadPicInfoList.size() - 1 && showDelete) {
                picUploadImg.setVisibility(View.GONE);
                delPicUploadImg.setVisibility(View.GONE);
                addPicUploadImg.setVisibility(View.VISIBLE);
            } else {
                Glide.with(context).load(picUrl).placeholder(R.mipmap.default_picture)
                        .bitmapTransform(new CenterCrop(context),new RoundedCornersTransformation(context, Util.dip2px(context, 4),
                                0,RoundedCornersTransformation.CornerType.ALL))
                        .error(R.mipmap.default_picture).thumbnail(0.3f).dontAnimate().into(picUploadImg);
                picUploadImg.setVisibility(View.VISIBLE);
                if (showDelete)
                    delPicUploadImg.setVisibility(View.VISIBLE);
                addPicUploadImg.setVisibility(View.GONE);
            }

            picUploadImg.setOnClickListener(this);  //删除
            delPicUploadImg.setOnClickListener(this);  //大图
            addPicUploadImg.setOnClickListener(this);  //系统相册
        }

        @Override
        public void onClick(View view) {
            int id = view.getId();
            if (id == R.id.img_pic_upload) {//系统相册
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(getAdapterPosition());
                }
            } else if (id == R.id.img_delete_upload_add) {  //大图
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(getAdapterPosition());
                }
            } else if (id == R.id.img_delete_upload) { //删除
                uploadPicInfoList.remove(getAdapterPosition());
                if(deletePicUrlListener != null){
                    deletePicUrlListener.deletePicUrl(getAdapterPosition());
                }
                notifyDataSetChanged();
            }
        }
    }

    private DeletePicUrlListener deletePicUrlListener;

    public void setDeletePicUrlListener(DeletePicUrlListener deletePicUrlListener) {
        this.deletePicUrlListener = deletePicUrlListener;
    }

    public interface DeletePicUrlListener{
        void deletePicUrl(int position);
    }

}
